package com.gitee.qdbp.jdbc.stream;

import java.util.Map;
import org.springframework.jdbc.core.RowMapper;
import com.gitee.qdbp.able.jdbc.paging.PageList;
import com.gitee.qdbp.able.jdbc.paging.Paging;
import com.gitee.qdbp.jdbc.api.SqlDao;

/**
 * 同时设置queryId/countId, 分页后续操作: list / resultAs
 *
 * @author zhaohuihua
 * @version 20210530
 */
public class SqlOnIdPairPagingStream {

    protected final SqlDao dao;
    protected final String queryId;
    protected final String countId;
    protected final Object params;
    protected final Paging paging;

    SqlOnIdPairPagingStream(SqlDao dao, String queryId, String countId, Object params, Paging paging) {
        this.dao = dao;
        this.queryId = queryId;
        this.countId = countId;
        this.params = params;
        this.paging = paging;
    }

    /**
     * 查询列表<br>
     * <pre>
    List&lt;Map&lt;String, Object&gt;&gt; users = qdbcBoot.sqlStream()
        .sqlId("SysUserMapper:queryRoleUsers", "SysUserMapper:countRoleUsers")
        .params(map)
        .pageBy(1,10)
        .list().asPartList(); // 分页后返回的是PageList, 需要转换为普通List
     * </pre>
     * 
     * @return 查询结果列表
     */
    public PageList<Map<String, Object>> list() {
        return this.dao.pageForMaps(queryId, countId, params, paging);
    }

    /**
     * 设置结果实体类型<br>
     * <pre>
    List&lt;SysUser&gt; users = qdbcBoot.sqlStream()
        .sqlId("SysUserMapper:queryRoleUsers", "SysUserMapper:countRoleUsers")
        .params(map)
        .pageBy(1,10)
        .resultAs(SysUser.class)
        .list().asPartList(); // 分页后返回的是PageList, 需要转换为普通List
     * </pre>
     * 
     * @param <T> 实体类型
     * @param resultType 实体类
     * @return 返回后续流式操作对象
     */
    public <T> SqlOnIdPairPagingResultStream<T> resultAs(Class<T> resultType) {
        return new SqlOnIdPairPagingResultStream<>(dao, queryId, countId, params, paging, resultType);
    }

    /**
     * 设置结果实体类型
     * 
     * @param <T> 实体类型
     * @param rowMapper 结果转换类
     * @return 返回后续流式操作对象
     */
    public <T> SqlOnIdPairPagingResultStream<T> resultAs(RowMapper<T> rowMapper) {
        return new SqlOnIdPairPagingResultStream<>(dao, queryId, countId, params, paging, rowMapper);
    }
}
